app.controller('couponsCtrl',['$scope','utilService','couponsService','toaster','$modal',
    function ($scope,utilService ,couponsService,toaster,$modal) {
    $scope.couponsVO={isChain:0};

    $scope.findAll=function(){
        utilService.queryForPagingList($scope,{
            url:$scope.app.domain + 'coupons',
            params:$scope.couponsVO
        }).then(function(result){
        });
    };

    $scope.updateCouponsStatus= function (id) {
        $scope.couponsId=id;
        couponsService.updateCouponsStatus($scope).then(function(result){
            switch (result.status){
                case 200:
                    toaster.pop('success', "提示", "操作成功");
                    $scope.findAll();
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        })
    };

    $scope.findAll();

    $scope.delCoupons=function(id){
        var param = {
            couponsId : id
        };
        // query data.
        openModal("确定要删除该优惠券吗？");
        function openModal(msg){
            utilService.modal($modal, $scope, {
                title:"message",
                controller:"delCouponsCtrl",
                message:msg,
                data:param
            });
        }
    }
}]);

app.controller('delCouponsCtrl', ['$scope', '$modalInstance','$state','couponsService','toaster','modalData',
    function($scope, $modalInstance,$state,couponsService,toaster,modalData) {
        $scope.modal = modalData;
        $scope.param = {
            couponsId:modalData.data.couponsId
        };
        $scope.isDelModal=true;

        $scope.ok = function(){
            if(!$scope.isDelModal){
                return;
            }
            $scope.isDelModal = false;
            couponsService.delCoupons($scope).then(function(result){
                switch (result.status){
                    case 200:
                        $modalInstance.dismiss('success');
                        toaster.pop('success', "提示", "操作成功");
                        $scope.isDelModal=true;
                        $scope.findAll();
                        break;
                    case 640:
                        $modalInstance.dismiss('error');
                        toaster.pop('error', "操作失败", "找不到该优惠劵!");
                        $scope.isDelModal = true;
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break;
                }
            });
        }
        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    }
]);